package com.jd.jr.insights.dm.dim.util.fastjson.tree;


import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;


/**
 * <p>Title: 树节点</p>
 * <p>Description:一棵树由许多包含父子关系的节点组成 </p>
 *
 * @author liuzhibo
 * @date 2017年1月18日
 */
public class TreeNode {
    //树节点ID
    @JSONField(ordinal = 1)
    private Integer id;
    //树节点名称
    @JSONField(ordinal = 2)
    private String name;
    //树节点编码
    @JSONField(ordinal = 3)
    private String nodeCode;
    //父节点ID
    @JSONField(ordinal = 4)
    private Integer parentNodeId;
    //节点在树中的排序号
    @JSONField(ordinal = 5)
    private Integer orderNum;
    //节点所在的层级
    @JSONField(ordinal = 6)
    private int level;
    private TreeNode parent;
    //当前节点的二子节点
    @JSONField(ordinal = 7)
    private List<TreeNode> children = new ArrayList<TreeNode>();
    //当前节点的子孙节点
    private List<TreeNode> allChildren = new ArrayList<TreeNode>();





    public TreeNode(ITreeNode obj) {
        this.id = obj.getNodeId();
        this.name = obj.getNodeName();
        this.nodeCode=obj.getNodeCode();
        this.parentNodeId = obj.getNodeParentId();
        this.orderNum = obj.getOrderNum();
        this.level=obj.getLevel();
    }

    public void addChild(TreeNode treeNode) {
        this.children.add(treeNode);
    }

    public void removeChild(TreeNode treeNode) {
        this.children.remove(treeNode);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer nodeId) {
        this.id = nodeId;
    }

    public String getName() {
        return name;
    }

    public void setName(String nodeName) {
        this.name = nodeName;
    }

    public Integer getParentNodeId() {
        return parentNodeId;
    }

    public void setParentNodeId(Integer parentNodeId) {
        this.parentNodeId = parentNodeId;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public TreeNode getParent() {
        return parent;
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

    public int getOrderNum() {
        return orderNum;
    }

    public void setOrderNum(int orderNum) {
        this.orderNum = orderNum;
    }

    public List<TreeNode> getAllChildren() {
        if (this.allChildren.isEmpty()) {
            for (TreeNode treeNode : this.children) {
                this.allChildren.add(treeNode);
                this.allChildren.addAll(treeNode.getAllChildren());
            }
        }
        return this.allChildren;
    }

    public String getNodeCode() {
        return nodeCode;
    }

    public void setNodeCode(String nodeCode) {
        this.nodeCode = nodeCode;
    }
}
